#!/bin/bash
#pMusic - build gtkdialog xml code
#GPL - Copyright Sigmund Berglund

export TEXTDOMAIN=pmusic
export OUTPUT_CHARSET=UTF-8

[ ! "$PLAYLIST_HEIGHT" ] && PLAYLIST_HEIGHT=150

GUI_PLAYLIST_FIELD='
<hbox '$ST'>
 <hbox '$ST'>
  <tree name="playlist" tooltip-column="5" headers_visible="false" exported_column="3" reorderable="true" hover-selection="true" column-visible="true|true|true|false" '$ST'>
   <label>A|B|C|D</label>
   <height>'$PLAYLIST_HEIGHT'</height><width>150</width>
   <input file stock-column="0">'$WORKDIR'/playlist</input>
   <output file>'$WORKDIR'/playlist_out</output> 
   <variable>PLAYLIST</variable>
   <action signal="button-press-event">grep -F "$PLAYLIST" '$WORKDIR'/playlist > '$WORKDIR'/PLAYLIST_PRESS_EVENT ;[ "$PLAYLIST" ] && . '$APPDIR'/func -playlist_mouse_press</action>
   <action signal="button-release-event" condition="command_is_true( echo $PTR_Y )">[ "$PLAYLIST" ] && . '$APPDIR'/func -playlist_mouse_release</action>
   <action signal="button-release-event" condition="command_is_true( echo $PTR_Y )">refresh:PLAYLIST</action>
   <action signal="button-release-event" condition="command_is_true( echo $PTR_Y )">break:</action>
   ### code below is activated when user moves track in queue. --> $PTR_Y gets 0
   <action signal="button-release-event">activate:PLAYLIST_SAVE</action> 
  </tree>
 </hbox>
 ### use a extern <button> to make user-defined <action> run _after_ builtin <action> (the opposite happens when a signal is defined).
 <button visible="false"> 
  <variable>PLAYLIST_SAVE</variable>
  <action>cp '$WORKDIR'/playlist '$WORKDIR'/playlist_backup</action> 
  <action>save:PLAYLIST</action>
  <action>cut -d"|" -f 2- '$WORKDIR'/playlist_out > '$WORKDIR'/playlist</action>
  ###run a check if moved item was dropped onto another item --> will delete the moved item --> we will then use last backup
  <action condition="command_is_true([[ $(grep -c / '$WORKDIR'/playlist_out) != $(grep -c / '$WORKDIR'/playlist_backup) ]] && cp '$WORKDIR'/playlist_backup '$WORKDIR'/playlist && echo true )">refresh:PLAYLIST</action> 
 </button> 
</hbox>'


S='
<hbox spacing="0" width-request="35" '$SF'>
 <hbox spacing="0" shadow-type="0" scrollable="true" vscrollbar-policy="2" width="35" width-request="10" '$SF'>
  <vbox spacing="0" '$ST'>'
   for I in 1 2 3 4 5 6 7 8 9; do
   S=$S'<vbox spacing="0" '
   [ -s $WORKDIR/PLAYLIST_${I}_NAME ] || S=$S'visible="false"'
   S=$S' '$SF'>
    <hbox height-request="1" '$ST'>
     <hseparator height-request="1" width-request="12" '$SF'></hseparator>
     <text '$ST'><label>""</label></text>
    </hbox>
    <eventbox name="playqueue_tab" '$ST'>
     <hbox>
      <vbox>
       <pixmap>
        <variable>PIX_TAB_'${I}'</variable>
        <width>20</width>
        <input file>'$WORKDIR'/PLAYLIST_'${I}'_ICON.svg</input>
       </pixmap>
      </vbox>
      <text name="playqueue_tab" xalign="0" angle="270" wrap="false" '$ST'>
       <variable>TXT_TAB_'${I}'</variable>
       <input>cat '$WORKDIR'/PLAYLIST_'$I'_NAME | sed -e "s%^%\n  %" -e "s%$%  %"</input>
      </text>
     </hbox>'
     S=$S'
     <action signal="enter-notify-event">disable:TXT_TAB_'${I}'</action>
     <action signal="leave-notify-event">enable:TXT_TAB_'${I}'</action>
     <action signal="button-press-event">if [[ $PTR_BTN -eq 3 ]]; then . '$APPDIR'/menu_playqueue_tabs '${I}'; else '$APPDIR'/func -playqueue_change '${I}'; fi</action>
     <action signal="button-release-event">activate:REFRESH_LIST</action>
     <action condition="command_is_true([[ $PTR_BTN -eq 3 ]] && echo true)" signal="button-release-event">activate:REFRESH_TABS</action>'
    S=$S'</eventbox>
    <variable>VBOX_TAB_'$I'</variable>
   </vbox>'
   done
   S=$S'
   <hseparator height-request="10" '$SF'></hseparator>
   <hbox '$SF'>
    <button relief="2" '$SF'>
     <input file stock="gtk-pmusic_add"></input>
     <action>rm -f '$WORKDIR'/xml_pmusic</action> ###to remove visible="true" for added tabs'
     for I in 9 8 7 6 5 4 3 2; do
      S=$S'
      <action condition="visible_is_true(VBOX_TAB_'$((${I}-1))')">gettext "Untitled" > '$WORKDIR'/PLAYLIST_'${I}'_NAME; echo -n > $WORKDIR/playlist_'${I}'; cp -f '$APPDIR'/themes/'$THEME'/blank.svg '$WORKDIR'/PLAYLIST_'${I}'_ICON.svg</action>
      <action condition="visible_is_true(VBOX_TAB_'$((${I}-1))')">refresh:TXT_TAB_'${I}'</action>
      <action condition="visible_is_true(VBOX_TAB_'$((${I}-1))')" function="show">"VBOX_TAB_'${I}'"</action>
      <action condition="visible_is_true(VBOX_TAB_'$((${I}-1))')" function="break">""</action>'
     done
    S=$S'</button>
   </hbox>
   ### global refresh
   <button visible="false">
    <variable>REFRESH_LIST</variable>
    <action>refresh:PLAYLIST</action>'
    for I in 1 2 3 4 5 6 7 8 9; do
     S=$S'<action>refresh:PIX_TAB_'${I}'</action>'
    done
   S=$S'</button>
   <button visible="false">
    <variable>REFRESH_TABS</variable>'
    for I in 1 2 3 4 5 6 7 8 9; do
     S=$S'
     <action>refresh:TXT_TAB_'${I}'</action>
     <action condition="command_is_true([[ ! -f '$WORKDIR'/PLAYLIST_'${I}'_NAME ]] && echo true)">hide:VBOX_TAB_'${I}'</action>'
    done
   S=$S'</button>
   ###---
  </vbox>
 </hbox>
 <variable>PLAYQUEUE_TABS</variable>
</hbox>'
GUI_PLAYLIST_TABS="$S"

GUI_BUTTON_PLAYMODE='
<button '$SF' tooltip-markup="'$(gettext '<b>Play mode</b>
Which track should be next')'">
 <input file>'$WORKDIR'/icon_playmode.svg</input>
 <height>22</height>
 <variable>PLAYMODE</variable>
 <action signal="button-press-event">. '$APPDIR'/menu_playmode</action>
 <action signal="button-press-event">refresh:PLAYMODE</action>
</button>'

GUI_BUTTON_PLAYLIST_HISTORY_BACK='
<button '$SF' tooltip-markup=" '$(gettext 'Go <b>back</b> in playQueue history')' ">
 <input file stock="gtk-pmusic_undo"></input>
 <action>. '$APPDIR'/func -undo</action>
 <action>Refresh:PLAYLIST</action>
</button>'

GUI_BUTTON_PLAYLIST_HISTORY_FORWARD='
<button '$SF' tooltip-markup=" '$(gettext 'Go <b>forward</b> in playQueue history')' ">
 <input file stock="gtk-pmusic_redo"></input>
 <action>. '$APPDIR'/func -redo</action>
 <action>Refresh:PLAYLIST</action>
</button>'

GUI_BUTTON_NEW='
<button '$SF' tooltip-text=" '$(gettext 'Clear playQueue')' ">
 <input file stock="gtk-pmusic_clear"></input>
 <action>'$APPDIR'/func_store_playlist &</action>
 <action>sleep 0.2; '$APPDIR'/func -new</action>
 <action>Refresh:PLAYLIST</action>
</button>'

GUI_SVG_NOWPLAYING='
<hbox scrollable="true" shadow-type="0" spacing="0" height="50" height-request="10" width-request="10" '$SF'>
  <pixmap height-request="50" '$SF'><variable>SVG_NOWPLAYING</variable><input file>'$WORKDIR'/status.svg</input></pixmap>
  <hbox height-request="1" '$ST'><text><label>""</label></text></hbox>
</hbox>
'
